﻿using System;
using System.ComponentModel;
using System.Data.EntityClient;
using System.Windows;
using Model;
using ModelInitializer;

namespace ModelUpdater
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            bool exists = false;
            ActualEntities context = null;
            do
            {
                Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
                ofd.AddExtension = true;
                ofd.DefaultExt = ".db3";
                ofd.Filter = "Adatbázis|*.db3";
                ofd.FilterIndex = 0;
                ofd.Title = "Modell tallózása";
                ofd.ValidateNames = true;
                ofd.Multiselect = false;
                ofd.CheckFileExists = true;
                ofd.CheckPathExists = true;
                bool? success = ofd.ShowDialog();
                if (success.HasValue && success.Value == true)
                {
                    EntityConnectionStringBuilder builder = new EntityConnectionStringBuilder();
                    builder.Metadata = "res://*/ActualModel.csdl|res://*/ActualModel.ssdl|res://*/ActualModel.msl";
                    builder.Provider = "System.Data.SQLite";
                    builder.ProviderConnectionString = "data source=\"" + ofd.FileName + "\"";
                    context = new ActualEntities(builder.ConnectionString);
                    exists = true;
                }
            }
            while (exists == false);
            Initializer init = new Initializer();
            init.ProgressChanged += new EventHandler<ProgressChangedEventArgs>(init_ProgressChanged);
            init.RefreshEffectMatrix(context, Analyzer.Properties.Resources.ISO9126toFxCop_EffectMatrix);
        }

        private void init_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            mMessagesListBox.Items.Add((e.UserState != null ? e.UserState.ToString() + " " : "") + e.ProgressPercentage.ToString());
        }
    }
}
